package com.it.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.it.JDBCUtils.JDBCUtils;
import com.it.Object.Connector;
import com.it.Object.Specification;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class Test {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @org.junit.Test
    public void test1() throws Exception {
        String url = "https://item.jd.com/100005855774.html";
        String number = url.substring(20).split("\\.")[0];
        System.out.println(number);
//        Document doc = Jsoup.parse(new URL(url), 10000);
//        String sel = "span.p-price > span.price.J-p-" + number;
//        System.out.println(sel);
//        System.out.println("hello" + text);
//        double price = Double.parseDouble(text.substring(1));
//        System.out.println(price);

//        Elements lis = doc.select("div.p-parameter > ul.parameter2.p-parameter-list > li");
//        for (Element li : lis) {
//            String s = li.text();
//            String title = li.attr("title");
//            if(s.contains("分辨率")) {
//                System.out.println(title);
//            }
//        }
    }

    @org.junit.Test
    public void test2() {
        List<Map<String, Object>> mapList =
                template.queryForList("select * from writers;");
        Integer id = null;
        for (Map<String, Object> map : mapList) {
            id = (Integer) map.get("id");
        }
        System.out.println(id);
    }

    @org.junit.Test
    public void getPrice() throws Exception {
        String url = "https://item.jd.com/100005855774.html";
        String number = url.substring(20).split("\\.")[0];
        String p = "https://p.3.cn/prices/mgets?skuIds=J_" + number;
        System.out.println(p);
        String json = loadJson(p).substring(1).split("]")[0];
        System.out.println(json);
        JSONObject jo = (JSONObject) JSON.parse(json);
        Double d = jo.getDouble("p");
        System.out.println(d);
    }

    //读取url上的json文件
    public static String loadJson(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    @org.junit.Test
    public void test3() {

        int i = Integer.parseInt("3个".split("个")[0]);
        System.out.println(i);
    }

    @org.junit.Test
    public void test4() throws IOException {
        Document doc = Jsoup.connect("https://item.jd.com/100022675586.html").get();
        Elements es = doc.select("div.Ptable-item");
        for (Element e : es) {
            if (e.select("h3").text().equals("接口")) {
                Connector con = new Connector();
                Elements dls = e.select("dl > dl.clearfix");
                for (Element dl : dls) {
                    if (dl.select("dt").text().equals("音频/耳机输出")) {
                        con.setAH(Integer.parseInt(dl.select("dd").text().split("个")[0]));
                    } else if (dl.select("dt").text().equals("DP")) {
                        con.setDP(Integer.parseInt(dl.select("dd").text().split("个")[0]));
                    } else if (dl.select("dt").text().equals("HDMI接口")) {
                        con.setHDMI(Integer.parseInt(dl.select("dd").text().split("个")[0]));
                    } else if (dl.select("dt").text().equals("VGA")) {
                        con.setVGA(Integer.parseInt(dl.select("dd").text().split("个")[0]));
                    } else if (dl.select("dt").text().equals("USB扩展/充电")) {
                        con.setUSB(Integer.parseInt(dl.select("dd").text().split("个")[0]));
                    } else if (dl.select("dt").text().equals("Type-C")) {
                        con.setTypeC(Integer.parseInt(dl.select("dd").text().split("个")[0]));
                    }
                }
                System.out.println(con.toString());
            }
        }
    }

    @org.junit.Test
    public void test5() throws IOException {
        Document doc = Jsoup.connect("https://item.jd.com/100022675586.html").get();
        Elements es = doc.select("div.Ptable-item");
        for (Element e : es) {
            if (e.select("h3").text().equals("规格")) {
                Specification sp = new Specification();
                Elements dls = e.select("dl > dl.clearfix");
                for (Element dl : dls) {
                    if (dl.select("dt").text().equals("产品尺寸（mm）")) {
                        sp.setProductSize(dl.select("dd").text());
                    } else if (dl.select("dt").text().equals("电源类型")) {
                        sp.setPower_Type(dl.select("dd").text());
                    } else if (dl.select("dt").text().equals("底座")) {
                        sp.setBase(dl.select("dd").text());
                    } else if (dl.select("dt").text().equals("产品净重（kg）")) {
                        sp.setWeight(dl.select("dd").text());
                    } else if (dl.select("dt").text().equals("壁挂规格")) {
                        sp.setWallSpec(dl.select("dd").text());
                    } else if (dl.select("dt").text().equals("是否支持壁挂")) {
                        sp.setIfHang(dl.select("dd").text());
                    }
                }
                System.out.println(sp.toString());
            }
        }

    }
}
